package com.example.hp.androidapplication;

import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private EditText infoEt;
    private Handler handler;

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        infoEt = (EditText) findViewById(R.id.info);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                String line = msg.getData().getString("line");
                infoEt.setText(line);
            }
        };
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    public void sendToServer(View view) {
        new Thread() {
            @Override
            public void run() {
                try {
                    send1("ce");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void send1(String info) throws Exception {
        Socket sk = new Socket("192.168.3.31", 6656);
        //Socket sk=new Socket("192.168.3.21",6688);
        InputStream in = sk.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = reader.readLine();
        Log.i("ce", line);
        Bundle bundle = new Bundle();
        bundle.putString("line", line);
        Message msg = new Message();
        msg.setData(bundle);
        handler.sendMessage(msg);
        reader.close();
        sk.close();
    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Main Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }

//    public void startToServer(View view) {
//        new Thread(){
//            @Override
//            public void run() {
//                super.run();
//                try {
//                    serve(7777);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }.start();
//    }

//    private void serve(int port)throws Exception{
//        ServerSocket server = new ServerSocket(port);
//        while (true) {
//            Socket socket = server.accept();
//            OutputStream out = socket.getOutputStream();
//            out.write("hello i am server\n".getBytes());
//            out.close();
//        }
//       // socket.close();
//    }

//    public void test1(View view) {
//        new Thread(){
//            @Override
//            public void run() {
//                try {
//                    catchWebData();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }.start();
//    }

//    private void catchWebData() throws Exception{
//        URL url = new URL("http://www.xjqd.sd.cn/");
//        HttpURLConnection connection = null;
//        connection = (HttpURLConnection)url.openConnection();
//        connection.setRequestMethod("GET");
//        connection.setRequestProperty("Accept", "text/html");
//
//        connection.setDoOutput(true);
//        connection.setDoInput(true);
//
//        InputStream in = connection.getInputStream();
//        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//        //byte[] bs = new byte[20];
//        String line = null ;
//
//        while ((line = reader.readLine()) != null) {
//            Log.i("kun",line);
//
//
//        }
//        reader.close();
//        connection.connect();
//    }
}
